import { Byte } from '../../api/io/Byte';
import { BriSkillEffectBase } from './BriSkillEffectBase';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**战报-连击属性枚举*/
export const enum E_BriDribble {
	/**基础信息*/
	BASE = 0,
	/**连击数*/
	DRIBBLE_COUNT = 1,
	/**效果类型*/
	EFFECT_TYPE = 2,
	/**枚举数量*/
	EnumCount = 3
}


/**战报-连击*/
export class BriDribble {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_BRI_DRIBBLE_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_BRI_DRIBBLE_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: BriDribble[] = [];

	/**创建入口*/
	public static create(json?: string): BriDribble {
		const c = this.POOL.pop() || new BriDribble();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**基础信息*/
	protected _base: BriSkillEffectBase = null;
	/**连击数 (uint8)*/
	protected _dribbleCount: number;
	/**效果类型 (uint8)*/
	protected _effectType: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (BriDribble.POOL.indexOf(this) === -1) {
			BriDribble.POOL.push(this);
		}
	}

	/**基础信息*/
	public get base() {
		return this._base;
	}


	/**连击数*/
	public get dribbleCount() {
		return this._dribbleCount;
	}


	/**连击数*/
	public set dribbleCount(v: number) {
		this._dribbleCount = v;
	}


	/**效果类型*/
	public get effectType() {
		return this._effectType;
	}


	/**效果类型*/
	public set effectType(v: number) {
		this._effectType = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_BriDribble): any {
		switch (k) {
			case E_BriDribble.BASE: return this._base;
			case E_BriDribble.DRIBBLE_COUNT: return this._dribbleCount;
			case E_BriDribble.EFFECT_TYPE: return this._effectType;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_BriDribble, v: any): void {
		switch (k) {
			case E_BriDribble.BASE: this._base = v; break;
			case E_BriDribble.DRIBBLE_COUNT: this._dribbleCount = v; break;
			case E_BriDribble.EFFECT_TYPE: this._effectType = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//基础信息
		if (this._base) {
			this._base.destroy();
			this._base = null;
		}
		if (!fromDestroy) {
			this._base = BriSkillEffectBase.create();
		}
		//连击数
		this._dribbleCount = 0;
		//效果类型
		this._effectType = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._base) {
			json.base = this._base.stringify(false);
		}
		if (this._dribbleCount) {
			json.dribbleCount = this._dribbleCount;
		}
		if (this._effectType) {
			json.effectType = this._effectType;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.base) {
			this._base.destroy();
			this._base = BriSkillEffectBase.create(json.base);
		}
		if (json.dribbleCount) {
			this._dribbleCount = json.dribbleCount;
		}
		if (json.effectType) {
			this._effectType = json.effectType;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		this._base.toBytes(byte);
		byte.writeUint8(this._dribbleCount);
		byte.writeUint8(this._effectType);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._base.fromBytes(byte);
		this._dribbleCount = byte.readUint8();
		this._effectType = byte.readUint8();
		return this;
	}


	/**克隆*/
	public clone(): BriDribble {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = BriDribble.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = BriDribble.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//基础信息
			if (idx === E_BriDribble.BASE) {
				const fui: string = us[i + 1];
				if (this._base) {
					this._base.processUpdate(fui);
				}
				else {
					console.error('BriDribble base is null when update.');
				}
				IAsync.dispatch(eventID, idx);
				i += 2;
				continue;
			}
			//连击数
			if (idx === E_BriDribble.DRIBBLE_COUNT) {
				const oldv = this._dribbleCount;
				this._dribbleCount = us[i + 1];
				IAsync.dispatch(eventID, idx, this._dribbleCount, oldv);
				i += 2;
				continue;
			}
			//效果类型
			if (idx === E_BriDribble.EFFECT_TYPE) {
				const oldv = this._effectType;
				this._effectType = us[i + 1];
				IAsync.dispatch(eventID, idx, this._effectType, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in BriDribble with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: BriDribble): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		//基础信息
		{
			const ui = this._base.getCompareInfo(other._base);
			if (ui && ui.length > 0) {
				updates.push(E_BriDribble.BASE, ui);
			}
		}
		if (this._dribbleCount !== other._dribbleCount) {
			updates.push(E_BriDribble.DRIBBLE_COUNT, this._dribbleCount);
		}
		if (this._effectType !== other._effectType) {
			updates.push(E_BriDribble.EFFECT_TYPE, this._effectType);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(BriDribble.TOTAL_EVENT_ID, this.getV(0));
	}

}
